﻿import re
import openpyxl


def dataYz(str) -> object:
    # 将字符串i以全部空白字符为分割符，将其分割成一个字符列表
    line = re.split(r'\s+', str)
    # 将字符列表用','拼接成一个新字符串
    new_line = ','.join(line)
    # 将新字符串尾部产生的','去掉
    new_line = new_line.strip(',')
    return re.split(',', new_line)


def find_line_number(filename):
    liens = []
    catchment = ""
    with open(filename, 'r', encoding='gbk') as file:
        for line_num, line in enumerate(file, start=1):
            if "<<< 子汇水面积" in line:
                data = dataYz(line)
                print(data)
                catchment = data[2]
                print(catchment)
            else:
                data = dataYz(line)
                print(data)




# 读取TXT，切分数据，保存到Excel
def txt_split_excel(path):  # tpath=txt文件路径,epath=excel文件路径
    data = open(path, encoding='gbk')  # 读取txt文件，设置编码
    subcatchment = ""
    for num, i in enumerate(data):
        # 将字符串i以全部空白字符为分割符，将其分割成一个字符列表
        line = re.split(r'\s+', i)
        # 将字符列表用','拼接成一个新字符串
        new_line = ','.join(line)
        # 将新字符串尾部产生的','去掉
        new_line = new_line.strip(',')
        print(re.split(',', new_line))


path = 'output_2.rpt'
find_line_number(path)
